/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gride.plugin.testplugin;


import gride.events.EventListener;
import gride.events.GrideEvents;
import gride.gui.GUIManager;
import gride.plugin.GridePlugin;
import gride.exceptions.UserCancelException;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 *
 * @author lukasz
 */
public class TestPlugin extends GridePlugin
{
    JButton myButton;
    JMenuItem myEntry1;
    JMenuItem myEntry2;

    EventListener exitListener;

    /**
     * 
     */
    @Override
    public void activate()
    {
        if (this.active)
            return;

        myButton = new JButton("plugin button");
        guiManager.addComponent(GUIManager.BOTTOM_PANE, myButton, "test plugin");

        myEntry1 = new JMenuItem("plugin 1");
        myEntry2 = new JMenuItem("Plugin 2");

        guiManager.addMenuItem(myEntry1, "Help");
        guiManager.addMenuItem(myEntry2, "Plugin");

        final Object myself = this;
        exitListener = new EventListener()
        {
            public Object whoIsListening()
            {
                return myself;
            }

            public void eventRaised(String eventID, Object[] args)
            {
                if (JOptionPane.showConfirmDialog(null, "Are you sure?") != JOptionPane.YES_OPTION)
                    throw new UserCancelException();
            }
        };

        eventManager.addListener(GrideEvents.EXITING_GRIDE, exitListener);

        this.active = true;
    }

    /**
     *
     */
    @Override
    public void deactivate()
    {
        if (!this.active)
            return;
        
        guiManager.removeComponent(myButton);

        guiManager.removeMenuItem(myEntry1);
        guiManager.removeMenuItem(myEntry2);

        eventManager.removeAllListenersFrom(this);

        this.active = false;
    }

}
